package hrm.action.org;

import hrm.action.Action;
import hrm.action.ActionForward;
import hrm.dto.org.OrgBaseDTO;
import hrm.dto.org.OrgViewDTO;
import hrm.serviceimple.OrgServiceImple;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class OrgPersonInfoAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		ActionForward actfoward  = new ActionForward();
		OrgServiceImple orgService = OrgServiceImple.getInstance();
		
		String orgcode = request.getParameter("code");
		String sdate = request.getParameter("sdate");
		orgcode = (orgcode==null)? "01000000":orgcode;
		if(sdate==null){
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			int month = cal.get(Calendar.MONTH)+1;
			int date = cal.get(Calendar.DATE);
			sdate = (year+"-"+month+"-"+date);
		}

		String orgtree = orgService.getMenuTreeString(sdate).toString(); 		
		
		OrgBaseDTO dto = orgService.getOrgbase(orgcode);
		OrgViewDTO vdto = new OrgViewDTO();
		vdto.setOrgcode(orgcode);
		vdto.setOrgofchief(null);
		List<OrgViewDTO> orgperson = orgService.getOrgView(vdto);
		String orguppername; 
		if(dto.getOrgcode().equals("01000000")){
			orguppername = "";			
		} else {
			orguppername = orgService.getOrgbase(dto.getOrguppercode()).getOrgname();			
		}
		
		request.setAttribute("orguppername", orguppername);
		request.setAttribute("orgtree", orgtree);
		request.setAttribute("orgbaseinfo", dto);
		request.setAttribute("orgperson", orgperson);
		request.setAttribute("sdate", sdate);

		actfoward.setUrl("orgpersoninfo");
		return actfoward;
	}

}
